package drds.data_propagate.binlog.event;

import drds.data_propagate.binlog.BinLogEvent;
import drds.data_propagate.binlog.Buffer;

/**
 * Append_block_log_event.
 *
 * @author <a href="mailto:changyuan.lh@taobao.com">Changyuan.lh</a>
 * @version 1.0
 */
public class AppendBlockEvent extends BinLogEvent {

    /* AB = "Append Block" */
    public static final int AB_FILE_ID_OFFSET = 0;
    private final Buffer buffer;
    private final int blockLen;
    private final long fileId;

    public AppendBlockEvent(Header header, Buffer buffer, FormatDescriptionEvent formatDescriptionEvent) {
        super(header);

        final int commonHeaderLen = formatDescriptionEvent.commonHeaderLength;
        final int postHeaderLen = formatDescriptionEvent.eventPostHeaderLength[header.eventType - 1];
        final int totalHeaderLen = commonHeaderLen + postHeaderLen;

        buffer.position(commonHeaderLen + AB_FILE_ID_OFFSET);
        fileId = buffer.getUint32();

        buffer.position(postHeaderLen);
        blockLen = buffer.limit() - totalHeaderLen;
        this.buffer = buffer.duplicate(blockLen);
    }

    public final long getFileId() {
        return fileId;
    }

    public final Buffer getBuffer() {
        return buffer;
    }

    public final byte[] getData() {
        return buffer.getData();
    }
}
